﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Brokers;
using System.Collections;
using ExpertMIS.Entities;

namespace ExpertMIS.Admin
{
    public partial class TroubleList : ExpertMIS.Codes.AdminAuthenticationPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            pgvTroubles.DeleteClientScript = "if(!confirm('您确定要删除该记录吗？')) return false; ";
            pgvTroubles.EditClientURL = "~/Admin/TroubleForm.aspx?oid={0}";
            pgvTroubles.AuditClientURL = "~/Admin/TroubleList.aspx?oid={0}&action=audit&passed={1}";
            pgvTroubles.DeleteClientURL = "~/Admin/TroubleList.aspx?oid={0}&action=delete";
            pgvTroubles.ObjectName = "ExpertMIS.TroublesListView";
            pgvTroubles.IsLoadData = true;

            if (!IsPostBack)
            {
                //// 这里需要再次判断，如果为客户端脚本制定window.location.href,则IsPostBack也为false
                //if (string.IsNullOrEmpty(Request["search"]))
                //{
                //    // 页面初次加载，则不需要load数据
                //    pgvTroubles.IsLoadData = false;
                //}
            }

            string action = Request["action"];
            if (!string.IsNullOrEmpty(action))
            {
                string oid = Request["oid"];
                string passed = Request["passed"];
                switch (action)
                {
                    case "audit":
                        bool bCheck = false;
                        if (passed.ToString() == "1" || passed.ToString().ToLower() == "true")
                        {
                            bCheck = true;
                        }
                        AuditTrouble(oid as string, bCheck);
                        break;
                    case "delete":
                        DeleteTrouble(oid);
                        break;
                    default:
                        break;
                }
            }
        }

        protected void AuditTrouble(string OID, bool checkedValue)
        {
            try
            {
                TroubleBroker broker = new TroubleBroker();
                Hashtable hsQuery = new Hashtable();
                hsQuery.Add("OID", OID);
                TroubleEntity entity = broker.GetEntity(hsQuery) as TroubleEntity;

                if (entity == null)
                {
                    return;
                }

                entity.IsPassed = !checkedValue;
                broker.SaveEntity(entity, null);
            }
            catch
            {
                return;
            }
        }

        protected void DeleteTrouble(string OID)
        {
            try
            {
                TroubleBroker broker = new TroubleBroker();
                Hashtable hsQuery = new Hashtable();
                hsQuery.Add("OID", OID);
                TroubleEntity entity = broker.GetEntity(hsQuery) as TroubleEntity;

                if (entity == null)
                {
                    return;
                }
                broker.RemoveEntity(entity, null);
            }
            catch
            {
                return;
            }
        }
    }
}
